﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.Domain.Suppliers;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.SupplierSearchWrite
{
    public class SupplierDeleteHandler : IRequestHandler<SupplierDeleteCommand, ResultsDTO>
    {
        private readonly IBateRepository<Supplier> _supplierRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierRepository"></param>
        public SupplierDeleteHandler(IBateRepository<Supplier> supplierRepository)
        {
            _supplierRepository = supplierRepository;
        }
        /// <summary>
        /// 供应商逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>

        public async Task<ResultsDTO> Handle(SupplierDeleteCommand request, CancellationToken cancellationToken)
        {
            var list = new ResultsDTO() { Code = 0 };

            //连接数据库
            DapperHelper dapper = new DapperHelper();
            var conn = dapper.MangoMesWrite;

            //实体
            var supplier = new Supplier();
            var str = "update supplier set SupplierStart=1 where SupplierId=" + request.SupplierId + "";
            var result = _supplierRepository.Add(str, supplier, conn);
            if (result != null)
            {
                list.Code = 1;
                list.Message = "删除成功";
            }
            return list;
        }
    }
}
